package com.qifei.xmlg_backend.service.impl;

import com.qifei.xmlg_backend.entity.Dept;
import com.qifei.xmlg_backend.mapper.DeptMapper;
import com.qifei.xmlg_backend.mapper.StatisticsMapper;
import com.qifei.xmlg_backend.service.IStatisticsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 统计数据服务实现类
 * </p>
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class StatisticsServiceImpl implements IStatisticsService {

    private final StatisticsMapper statisticsMapper;
    private final DeptMapper deptMapper;

    @Override
    public Map<String, Object> getEmpGenderStats() {
        List<Map<String, Object>> stats = statisticsMapper.getEmpGenderStats();
        
        List<String> labels = new ArrayList<>();
        List<Integer> values = new ArrayList<>();
        
        for (Map<String, Object> stat : stats) {
            Integer gender = (Integer) stat.get("gender");
            Long count = (Long) stat.get("count");
            
            // 性别: 1 男, 2 女
            if (gender == 1) {
                labels.add("男");
            } else if (gender == 2) {
                labels.add("女");
            } else {
                labels.add("未知");
            }
            
            values.add(count.intValue());
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("labels", labels);
        result.put("values", values);
        
        return result;
    }

    @Override
    public Map<String, Object> getEmpDeptStats() {
        // 获取所有部门
        List<Dept> depts = deptMapper.selectList(null);
        
        List<String> deptNames = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();
        
        // 获取每个部门的员工数量
        for (Dept dept : depts) {
            deptNames.add(dept.getName());
            
            // 获取该部门的员工数量
            int count = statisticsMapper.getEmpCountByDeptId(dept.getId());
            counts.add(count);
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("deptNames", deptNames);
        result.put("counts", counts);
        
        return result;
    }

    @Override
    public Map<String, Object> getEmpEntryDateStats() {
        List<Map<String, Object>> stats = statisticsMapper.getEmpEntryDateStats();
        
        // 初始化12个月的数据
        List<String> months = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月");
        Integer[] counts = new Integer[12];
        Arrays.fill(counts, 0);
        
        // 填充实际数据
        for (Map<String, Object> stat : stats) {
            Integer month = (Integer) stat.get("month");
            Long count = (Long) stat.get("count");
            
            // 月份从1开始，数组索引从0开始
            if (month >= 1 && month <= 12) {
                counts[month - 1] = count.intValue();
            }
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("months", months);
        result.put("counts", Arrays.asList(counts));
        
        return result;
    }
} 